package com.ruoyi.business.service.impl;

import java.text.SimpleDateFormat;
import java.time.LocalTime;
import java.util.Date;
import java.util.List;

import com.ruoyi.business.service.IGwCarService;
import com.ruoyi.business.service.IGwPersonalService;
import com.ruoyi.common.core.domain.entity.GwBooking;
import com.ruoyi.common.core.domain.entity.GwCar;
import com.ruoyi.common.core.domain.entity.GwPersonal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.business.mapper.GwBookingMapper;

import com.ruoyi.business.service.IGwBookingService;

/**
 * 车辆管理Service业务层处理
 *
 * @author ruoyi
 * @date 2024-07-24
 */
@Service
public class GwBookingServiceImpl implements IGwBookingService
{
    @Autowired
    private GwBookingMapper gwBookingMapper;
    @Autowired
    private IGwPersonalService gwPersonalService;
    @Autowired
    private IGwCarService gwCarService;
    /**
     * 查询车辆管理
     *
     * @param bookingId 车辆管理主键
     * @return 车辆管理
     */
    @Override
    public GwBooking selectGwBookingByBookingId(Long bookingId)
    {
        return gwBookingMapper.selectGwBookingByBookingId(bookingId);
    }

    /**
     * 查询车辆管理列表
     *
     * @param gwBooking 车辆管理
     * @return 车辆管理
     */
    @Override
    public List<GwBooking> selectGwBookingList(GwBooking gwBooking)
    {
        return gwBookingMapper.selectGwBookingList(gwBooking);
    }

    /**
     * 新增车辆管理
     *
     * @param gwBooking 车辆管理
     * @return 结果
     */
    @Override
    public int insertGwBooking(GwBooking gwBooking)
    {
        if (gwBooking.getBookingStatus()==1){

        }else {
            //现场取号
            gwBooking.setArriveDate( new Date() );
            LocalTime now = LocalTime.now();
            gwBooking.setArriveDateType(now.getHour() < 12 ? 1L : 2L );
        }
        //个人信息
        GwPersonal gwPersonal = gwPersonalService.selectGwPersonalByPersonalId(gwBooking.getPersonalId());
        if (gwPersonal != null){
            gwBooking.setUserName( gwPersonal.getUserName() );
            gwBooking.setUserId( gwPersonal.getUserId() );
            gwBooking.setPersonalId( gwPersonal.getPersonalId() );
            gwBooking.setPhoneNumber( gwPersonal.getPhoneNumber() );
            gwBooking.setIdcardUrlA( gwPersonal.getPersonalUrlA() );
            gwBooking.setIdcardUrlB( gwPersonal.getPersonalUrlB() );
        }else {
            throw new RuntimeException("出错啦，请稍后重试！!");
        }
        //车辆信息
        GwCar gwCar = gwCarService.selectGwCarByCarId(gwBooking.getCarId());
        if (gwCar != null){
            gwBooking.setCarId( gwCar.getCarId() );
            gwBooking.setCarNum( gwCar.getCarNum() );
        }else {
            throw new RuntimeException("出错啦，请稍后重试！!");
        }
        //生成号码
        /*String bookingNum = "";
        Date arriveDate = gwBooking.getArriveDate();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        GwBooking gwBooking1 = new GwBooking();
        gwBooking1.setArriveDate( gwBooking.getArriveDate() );
        int num = gwBookingMapper.selectGwBookingCount(gwBooking1)+1;
        if (gwBooking.getCarType()==1){
            bookingNum = "重车A"+sdf.format(arriveDate)+num;
            //判断号码是否唯一
            gwBooking1.setBookingNum( bookingNum );
            List<GwBooking> gwBookingList = gwBookingMapper.selectGwBookingList(gwBooking1);
            if (gwBookingList.size()>0){
                //重新获取
            }
            gwBooking.setBookingNum( bookingNum );
        }else {
            bookingNum = "空车A"+sdf.format(arriveDate)+num;
            //判断号码是否唯一
            gwBooking1.setBookingNum( bookingNum );
            List<GwBooking> gwBookingList = gwBookingMapper.selectGwBookingList(gwBooking1);
            if (gwBookingList.size()>0){
                //重新获取
            }
            gwBooking.setBookingNum( bookingNum );
        }*/
        return gwBookingMapper.insertGwBooking(gwBooking);
    }

    @Override
    public int takeNumber(GwBooking gwBooking)
    {
        String bookingNum = "";
        Date arriveDate = gwBooking.getArriveDate();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        GwBooking gwBooking1 = new GwBooking();
        gwBooking1.setArriveDate( gwBooking.getArriveDate() );
        int num = gwBookingMapper.selectGwBookingCount(gwBooking1)+1;
        if (gwBooking.getCarType()==1){
            bookingNum = "重车A"+sdf.format(arriveDate)+num;
            //判断号码是否唯一
            gwBooking1.setTakeNum( bookingNum );
            List<GwBooking> gwBookingList = gwBookingMapper.selectGwBookingList(gwBooking1);
            if (gwBookingList.size()>0){
                //重新获取
            }
            gwBooking.setTakeNum( bookingNum );
        }else {
            bookingNum = "空车A"+sdf.format(arriveDate)+num;
            //判断号码是否唯一
            gwBooking1.setTakeNum( bookingNum );
            List<GwBooking> gwBookingList = gwBookingMapper.selectGwBookingList(gwBooking1);
            if (gwBookingList.size()>0){
                //重新获取
            }
            gwBooking.setTakeNum( bookingNum );
        }
        return gwBookingMapper.updateGwBooking(gwBooking);
    }

    /**
     * 修改车辆管理
     *
     * @param gwBooking 车辆管理
     * @return 结果
     */
    @Override
    public int updateGwBooking(GwBooking gwBooking)
    {
        return gwBookingMapper.updateGwBooking(gwBooking);
    }

    /**
     * 批量删除车辆管理
     *
     * @param bookingIds 需要删除的车辆管理主键
     * @return 结果
     */
    @Override
    public int deleteGwBookingByBookingIds(Long[] bookingIds)
    {
        return gwBookingMapper.deleteGwBookingByBookingIds(bookingIds);
    }

    /**
     * 删除车辆管理信息
     *
     * @param bookingId 车辆管理主键
     * @return 结果
     */
    @Override
    public int deleteGwBookingByBookingId(Long bookingId)
    {
        return gwBookingMapper.deleteGwBookingByBookingId(bookingId);
    }
}
